package com.moyq5.mvc.framework.utils;

/**
 * @author Moyq5
 * @date 2018年8月14日
 */
public abstract class StringUtils {

	private final static char[] chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	
	public static String random(int length) {
		final int bound = chars.length - 1;
		String str = "";
		for (int i = 0; i < length; i++) {
			int r = new Double(Math.random() * bound).intValue();
			str += chars[r];
		}
		return str;
	}
	
	public static boolean isEmpty(Object str) {
		return (str == null || "".equals(str));
	}
	

	public static Long[] toLongArray(String string, String regex) {
        if (null == string || string.isEmpty()) {
            return null;
        }
        String[] src = string.split(regex);
        Long[] dest = new Long[src.length];
        boolean isEmpty = true;
        for (int i = 0; i < src.length; i++) {
            try {
                dest[i] = Long.parseLong(src[i]);
                isEmpty = false;
            } catch (NumberFormatException e) {
                dest[i] = 0L;
            }
        }
        if (isEmpty) {
            return null;
        }
        return dest;
    }

    public static String toString(Long[] array, String join) {
        if (null == array || array.length == 0) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (null != array[i]) {
                if (i > 0) {
                    sb.append(join);
                }
                sb.append(array[i]);
            }
        }
        return sb.toString();
    }
}
